package com.bicon.botu.mall.member.service.impl;

import com.bicon.botu.mall.member.service.IBrowsingHistory;
import com.bicon.botu.mall.member.vo.ProductVo;
import com.bicon.botu.core.base.common.utils.StringUtils;
import com.bicon.botu.mall.member.common.MemberConstants;
import com.bicon.botu.mall.member.mapper.AttentionMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 李双喜
 * @Date 2017/8/22 11:28
 */
@Service
@Slf4j
@Transactional
public class BrowsingHistoryImpl implements IBrowsingHistory {
    @Autowired
    private AttentionMapper attentionMapper;
    /**
     * 看相似（根据商品分类相似和价格相似查询）
     */
    @Override
    public List<ProductVo> selectResemble(String categoryId, BigDecimal price) {
        Map<String, Object> map = new HashMap<>();
        //将double转为BigDecimal
        BigDecimal similarPrice = new BigDecimal(MemberConstants.SIMILARPRICE);
        //BigDecimal作加减
        BigDecimal priceUpper = price.add(similarPrice);
        BigDecimal priceLower = price.subtract(similarPrice);

        map.put("categoryId", categoryId);
        map.put("priceUpper", priceUpper);
        map.put("priceLower", priceLower);
        List<ProductVo> list = attentionMapper.selectResemble(map);
        //判断不为空
        if (CollectionUtils.isEmpty(list)) {
           return null;
        }
        return list;//这里返回6条
    }
    /**
     * 浏览历史--为你推荐
     * @param categoryIdJson    商品类目id
     * @return                  productVos
     */
    @Override
    public List<ProductVo> selectRecommend(String categoryIdJson) {
        if (StringUtils.isEmpty(categoryIdJson)){
            return null;
        }
        List<ProductVo> productVos = new ArrayList<>();
        String[] categoryIds = categoryIdJson.split(",");
        for(int i =0;i<categoryIds.length;i++){
            List<ProductVo> list = attentionMapper.selectAttentionRecommend(categoryIds[i]);
            productVos.addAll(list);
        }
        if (!CollectionUtils.isEmpty(productVos)){
            return productVos;
        }
        return null;
    }
}
